﻿
<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>e-wyszukiwarka</title>
		<link rel="stylesheet" type="text/css" media="all" href="resources/css/jsDatePick_ltr.min.css" />
		<script type="text/javascript" charset="utf-8" src="resources/js/jsDatePick.min.1.3.js"></script>
		<script type="text/javascript" charset="utf-8" src="resources/js/jquery.js" ></script>
		<!--script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script-->

		<script type="text/javascript" charset="utf-8">

			debug = false;

			init = function() {
				new JsDatePick({
					useMode:2,
					target:"inputDate",
					dateFormat:"%Y-%m-%d",
					imgPath:"resources/img/",
					cellColorScheme:"beige"
					/*selectedDate:{				This is an example of what the full configuration offers.
						day:5,						For full documentation about these settings please see the full version of the code.
						month:9,
						year:2006
					},
					yearsRange:[1978,2020],
					limitToToday:false,
					dateFormat:"%m-%d-%Y",
					weekStartDay:1*/
				});
			};

			function decode_utf8( s ) {

			//	s = s.replace(/Ä™/g, "ę");
			//	s = s.replace(//g, "Ę"); // don't occur
			//	s = s.replace(/Ăł/g, "ó");
			//	s = s.replace(/Ă“/g, "Ó"); // don't occur
			//	s = s.replace(/Ä…/g, "ą");
			//	s = s.replace(/Ä„/g, "Ą"); // don't occur
			//	s = s.replace(/Ĺ›/g, "ś");
			//	s = s.replace(/Ĺš/g, "Ś");
			//	s = s.replace(/Ĺ‚/g, "ł");
//			//	s = s.replace(/Ĺ/g, "Ł");
//				s = s.replace(/Ĺź/g, "ż");
//				s = s.replace(/Ĺş/g, "ź");
//				s = s.replace(/Ĺź/g, "Ż");
//				s = s.replace(/Ĺş/g, "Ź");
//				s = s.replace(/Ä‡/g, "ć");
//				s = s.replace(/Ä†/g, "Ć");
//				s = s.replace(/Ĺ„/g, "ń");
				//s = s.replace(//g, "Ń"); // can't find representation
				return s;
			}

			function performSearch() {
				
				document.getElementById("search_result").innerHTML = "Wyszukuję najlepsze trasy";
				
				url='rest/findConnections';
				
				$.ajax({
					url: 'rest/findConnections',
					contentType: "application/x-www-form-urlencoded;charset=UTF-8",
					data: ({starting_point : document.forms['user_input'].starting_point.value,
							destination    : document.forms['user_input'].destination.value,
							starting_date  : document.forms['user_input'].date.value + " " + document.forms['user_input'].time.value}),
					
					success: function(data) {
						
						resultString="";
						
						for (c=0; c<data.connections.length; c++) {
							resultString += "<big><b>Połączenie nr " + (c+1) + " </b></big><br>";
							for (i=0; i<data.connections[c].length; i++) {
								resultString += i+1 + ": " + data.connections[c][i].carrier;
								resultString += " ( " + data.connections[c][i].model + " )";
								resultString += "<br>";
								resultString += data.connections[c][i].starting_point;

							//	if (i==0)
									resultString += " ( " + formatDate(data.connections[c][i].starting_date, true) + " )";
							//	else
							//		resultString += " ( " + formatDate(data.connections[c][i].starting_date) + " )";

								resultString += " -> ";

								resultString += data.connections[c][i].destination;

								resultString += " ( " + formatDate(data.connections[c][i].ending_date, true) + " )";

								resultString += "<br>";

								if (data.connections[c][i].stops.length > 0) {
									resultString += "Dodatkowe przystanki: ";
									for(s=0; s<data.connections[c][i].stops.length; s++)
										resultString += data.connections[c][i].stops[s] + ", ";
										resultString += "<br>";
								}
								resultString += "<small>";
								resultString += data.connections[c][i].notes + "</small><br><br>";
							} // i loop
						} // c loop
						resultString += data.error;
						//document.getElementById("search_result").innerHTML = decode_utf8(resultString);
						document.getElementById("search_result").innerHTML = resultString;
					}, // ajax success function

					error: function(request, status, error) {
						alert('AJAX error function: ' + request + ' : ' + status + ' : ' + error);
						// full response with errors: request.responseText
					} // ajax error function
				}); // ajax
			} // function performSearch

			function formatDate(/*string timestamp*/ ts, /*boolean*/ withDate /*= false*/) {
				
				var date = new Date(parseInt(ts));
				
				if ( isNaN(date.valueOf()) )
					return "Bład serwera";
				
				if (debug)
					alert("Timestamp:  " + ts +
						"\nDate Local: " + date +
						"\nDate UTC:   " + date.toUTCString());
						
				var hours = (date.getUTCHours() < 10) ? "0" + date.getUTCHours() : date.getUTCHours();
				var minutes = (date.getUTCMinutes() < 10) ? "0" + date.getUTCMinutes() : date.getUTCMinutes();
				var resultString = "";
				
				if (!withDate || withDate==false) {
					resultString = hours + ":" + minutes;
				}
				else {
					var day = (date.getUTCDate() < 10) ? "0" + date.getUTCDate() : date.getUTCDate();
					var month = (date.getUTCMonth() < 10) ? "0" + (date.getUTCMonth() + 1) : (date.getUTCMonth() + 1);
					var year = date.getUTCFullYear();
					resultString = day + "-" + month + "-" + year + " " + hours + ":" + minutes;
				}
				
				return resultString;
			}
			
			function changeDebug() {
				debug = 1 - debug;
			}
			
			function resetInput() {
				//http://wowjava.wordpress.com/2010/11/11/autocomplete-textbox-in-jsp/
				var currentTime = new Date();
				var year = currentTime.getFullYear();
				var month = currentTime.getMonth() + 1;
				if (month < 10)
					month = "0" + month;
				var day = currentTime.getDate();
				if (day < 10)
					day = "0" + day;
				var hours = currentTime.getHours();
				if (hours < 10)
					hours = "0" + hours;
				var minutes = currentTime.getMinutes();
				if (minutes < 10)
					minutes = "0" + minutes;
				document.forms['user_input'].starting_point.value = 'Tczew';
				document.forms['user_input'].destination.value = 'Skowarcz';
				document.forms['user_input'].time.value = hours + ":" + minutes;
				document.forms['user_input'].date.value = year + "-" + month + "-" + day;
				document.forms['user_input'].time_interpretation.value = 'departure';
				document.getElementById("search_result").innerHTML = ".";
			}
		</script>
    </head>
    <body onload="resetInput(); init()" background="resources/img/lgrey016.jpg">
    <div id="content">
		<div id="header" align="center" style="margin:5px">
			<h1>Witaj na e-wyszukiwarce połączeń krajowych</h1>
		</div id="header">
		<div id="main" style="margin:15px">
			<div id="search_input">
				<form name="user_input">
				<table border="0">
				<tr>
					<th>Z:</th>
					<th><input type="text" id="starting_point" value="Tczew"/></th>
					<td>Data:</td>
					
					<td><input id="inputDate" type="text" name="date" value="2012" /></td>
					<td><input type="radio" name="time_interpretation" value="departure" CHECKED /> Czas odjazdu </td>
				</tr>
				<tr>
					<td>Do:</td>
					<td><input type="text" name="destination" value="Skowarcz"/></td>
					<td>Czas:</td>
					<td><input type="text" name="time" value="20:12"/></td>
					<td><input type="radio" name="time_interpretation" value="arrival" /> Czas przyjazdu </td>
				</tr>
				</table>
				</form>
				<br>
				<button type="search" onclick="performSearch();"><h2> Wyszukaj </h2></button>
				<button type="search" onclick="resetInput();"> Wyczyść </button>
				<input type="checkbox" name="debug" value="debug" onclick="changeDebug();"/> Debug
				<br><br>
				
			</div id="search_input">
			<div id="search_result">
				
			</div id="search_result">
		</div id="main">
		<div id="footer" style="position:fixed; bottom:0px; width:100%; margin:5px" align="center">
			<small>
			Strona stworzona przez:
				Joannę Kacką,
				Pawła Barszcza,
				Marcina Lewandowskiego,
				Krystiana Liebera,
				Jakuba Pawłota,
				Michała Staronia,
				Macieja Szawłowskiego
			</small>
		</div id="footer">
    </div id="content">
    </body>
</html>
